//食物链
#include <iostream>
using namespace std;
const int MAXN=50000;

int color[MAXN+1];
struct node
{
    int head,next,last,size;
};
struct UFS
{
    node s[MAXN+1];
    void init(){
        for(int i=1;i<=MAXN;i++){
            s[i]={i,0,i};
        }
    }
    int find_set(int x){
        return s[x].head;
    }
    void addto(int hx,int hy,int colorinc){
        s[s[hx].last].next=hy;
        s[hx].last=s[hy].last;
        while (hy>0){
            s[hy].head=hx;
            color[hy]+=colorinc;
            hy=s[hy].next;
        }
    }
    void union_set(int x,int y,int rela){
        int colorinc=rela==1?color[x]-color[y]:color[x]+1-color[y];//1代表同类，2代表x吃y
        int hx=find_set(x);
        int hy=find_set(y);
        addto(hx,hy,colorinc);
    }
};
UFS ufs;
int n,k;
int ans=0;
int main(){
    ufs.init();
    cin>>n>>k;
    for(int i=1;i<=k;i++){
        int d,x,y;
        cin>>d>>x>>y;
        if (x>n||y>n) ans++;
        else if (d==2&&x==y) ans++;
        else {
            int hx=ufs.find_set(x);
            int hy=ufs.find_set(y);
            if (hx==hy){
                if (d==1&&(color[x]-color[y])%3!=0) ans++;
                else if (d==2&&(color[x]+1-color[y])%3!=0) ans++;
            }else {
                ufs.union_set(x,y,d);
            }
        }
    }
    cout<<ans;
}


